<?php
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: http://'.$_SERVER["HTTP_HOST"].''); exit; }

require('puslapiai/dievai/tools/list.class.php');
$sortableLists = new SLLists('javascript/scriptaculous/');	// points to path of scriptaculous JS files

$listItemFormat = '<li id="item_%s"><strong>%s</strong> <a href="?id,46;a,9;r,%s" style="align:right">[r]</a> <a href="?id,46;a,9;d,%s" style="align:right" onClick="return confirm(\'Ar tikrai trinti panelę?\')">[d]</a> <a href="?id,46;a,9;e,%s" style="align:right">[e]</a></li>';  // two arguments are the idField and the displayField

$sortableLists->addList('kaire','paneles_kaire');
$sortableLists->addList('desine','paneles_desine');

if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) {
	mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."panel` WHERE `id`= ".escape((int)$url['d'])." LIMIT 1") or die(mysql_error());
	redirect("?id,46;a,9","header");
}
//naujos paneles sukurimas
elseif (isset($url['n']) && $url['n'] == 1) {
	if (isset($_POST['Nauja_panele']) && $_POST['Nauja_panele'] == "Sukurti") {
		$panel = input($_POST['Panel']);
		$file = input(basename($_POST['File']));
		if (!file_exists("paneles/".$file)) { klaida("Klaida","<h3>Panelės failas <font color='red'>".$file."</font> neegzistuoja.</h3>Atsiprašome patikrinkite ar įvesti duomenys teisingi. Jei taip įsitikinkite ar <b>paneles/</b> direktorijoje toks failas egzistuoja"); }
		else {
			if (empty($panel) || $panel =='') { $panel = basename($file, ".php"); }
			$align = input($_POST['Align']); if (strlen($align)>1) { $align = 'L'; }
			$show = input($_POST['Show']); if (strlen($show)>1) { $align = 'Y'; }
			$sql = "INSERT INTO `".LENTELES_PRIESAGA."panel` (`panel`, `file`, `place`, `align`, `show`) VALUES (".escape($panel).", ".escape($file).", '0', ".escape($align).", ".escape($show).")";
			mysql_query1($sql) or die(mysql_error());
			redirect("?id,46;a,9","header");
		}
	}
	$failai = getFiles('paneles/');
	//print_r($failai);

	foreach ($failai as $file) {
		if ($file['type'] == 'file') {
			$sql = mysql_query1("SELECT panel FROM `".LENTELES_PRIESAGA."panel` WHERE file=".escape(basename($file['name']))." LIMIT 1");
			if (mysql_num_rows($sql) == 0) {
				$paneles[basename($file['name'])] = basename($file['name']).": " . $file['sizetext'] . "\n";
			}
		}
	}

	if (!isset($paneles) || count($paneles) < 1) { klaida("Klaida","<h3>Nenaudojamų panelių nėra</h3>Norėdami į svetainę įdėti naują panelę, pasinaudodami ftp uploadinkite naują php failą į [paneles/] direktoriją"); }
	else {
		$panele = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"new_panel"),
			"Panelės avadinimas:"=>array("type"=>"text","value"=>"Nauja panelė","name"=>"Panel","style"=>"width:400px"),
			"Panelės failas:"=>array("type"=>"select","value"=>$paneles,"name"=>"File"),
			"Kurioje pusėje:"=>array("type"=>"select","value"=>array("L"=>"Kairė","R"=>"Dešinė"),"name"=>"Align"),
			"Ar rodyti pavadinimą?"=>array("type"=>"select","value"=>array("Y"=>"Taip","N"=>"Ne"),"name"=>"Show"),
			""=>array("type"=>"submit","name"=>"Nauja_panele","value"=>"Sukurti")
		);
		
		include_once("priedai/class.php");
		$bla = new forma();
		lentele("Nauja panelė",$bla->form($panele,"Naujos panelės sukūrimas"));
	}
}

//Paneles redagavimas
elseif (isset($url['r']) && isnum($url['r']) && $url['r'] > 0) {
	if (isset($_POST['Redaguoti_panele']) && $_POST['Redaguoti_panele'] == "Redaguoti") {
		$panel = input($_POST['Panel']);
		if (empty($panel) || $panel =='') { $panel = "Nauja panelė"; }
		$align = input($_POST['Align']); if (strlen($align)>1) { $align = 'L'; }
		$show = input($_POST['Show']); if (strlen($show)>1) { $align = 'Y'; }
		$sql = "UPDATE `".LENTELES_PRIESAGA."panel` SET `panel`=".escape($panel).", `align`=".escape($align).", `show`=".escape($show)."  WHERE `id`=".escape((int)$url['r']);
		mysql_query1($sql);
		redirect("?id,46;a,9","header");
	}
	else {
		$sql = "SELECT * FROM `".LENTELES_PRIESAGA."panel` WHERE `id`=".escape((int)$url['r'])." LIMIT 1";
		$sql = mysql_fetch_assoc(mysql_query1($sql));
		$panele = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"new_panel"),
			"Panelės avadinimas:"=>array("type"=>"text","value"=>input($sql['panel']),"name"=>"Panel","style"=>"width:100%"),
			"Kurioje pusėje:"=>array("type"=>"select","value"=>array("L"=>"Kairė","R"=>"Dešinė"),"selected"=>input($sql['align']),"name"=>"Align"),
			"Ar rodyti rėmelį?"=>array("type"=>"select","value"=>array("Y"=>"Taip","N"=>"Ne"),"selected"=>input($sql['show']),"name"=>"Show"),
			""=>array("type"=>"submit","name"=>"Redaguoti_panele","value"=>"Redaguoti")
		);
		
		include_once("priedai/class.php");
		$bla = new forma();
		lentele(input($sql['file']." - ".$sql['panel']),$bla->form($panele,"Panelės redagavimas"));
	}
}

//Redaguojam panelės turinį
elseif (isset($url['e']) && isnum($url['e']) && $url['e'] > 0) {
	$panel_id = (int)$url['e'];	//Panelės ID

	if (isset($_POST['Redaguoti_txt']) && $_POST['Redaguoti_txt'] == "Redaguoti") {
		$sql = "SELECT `file` FROM `".LENTELES_PRIESAGA."panel` WHERE `id`=".escape($panel_id)." LIMIT 1";
		$sql = mysql_fetch_assoc(mysql_query1($sql));
		irasom('paneles/'.$sql['file'],$_POST['Turinys']);
	}
	else {
		$sql = "SELECT `id`, `panel`, `file` FROM `".LENTELES_PRIESAGA."panel` WHERE `id`=".escape($panel_id)." LIMIT 1";
		$sql = mysql_fetch_assoc(mysql_query1($sql));
		include 'paneles/'.$sql['file'];
		$paneles_txt = $text;
		echo <<<HTML
				<script language="JavaScript">
		// Notice: The simple theme does not use all options some of them are limited to the advanced theme
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			mode : "exact",
			elements : "Turinys",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false,
			theme_advanced_path_location : "bottom",
			plugins : "emotions,advimage",
			theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink",
			//theme_advanced_buttons2 : "",
			theme_advanced_buttons2_add : "emotions,media",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
			theme_advanced_disable : "help"
		});
		</script>
HTML;
		$panele = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"panel_txt"),
			//"Panelės avadinimas:"=>array("type"=>"text","value"=>input($sql['panel']),"name"=>"Panel","disabled"=>"disabled","style"=>"width:100%"),
			"Panelės turinys"=>array("type"=>"textarea","value"=>$paneles_txt,"name"=>"Turinys","style"=>"width:100%","rows"=>"10"),
			""=>array("type"=>"submit","name"=>"Redaguoti_txt","value"=>"Redaguoti")
		);
		
		include_once("priedai/class.php");
		$bla = new forma();
		lentele(input($sql['file']." - ".$sql['panel']),$bla->form($panele,"Panelės turinio redagavimas"));
	}
}
//Paneliu lygiavimas
elseif (isset($_POST['sortableListsSubmitted']) && !isset($url['n'])) {
	$orderArray = SLLists::getOrderArray($_POST['paneles_kaire'],'kaire');
	foreach($orderArray as $item) {
		$sql = "UPDATE `".LENTELES_PRIESAGA."panel` set place=".escape($item['order'])." WHERE `id`=".escape($item['element']);
		mysql_query1($sql);
	}
	$orderArray = SLLists::getOrderArray($_POST['paneles_desine'],'desine');
	foreach($orderArray as $item) {
		$sql = "UPDATE `".LENTELES_PRIESAGA."panel` set place=".escape($item['order'])." WHERE `id`=".escape($item['element']);
		mysql_query1($sql);
	}
	redirect("?id,46;a,9","header");
}

//atvaizduojam paneles
	$sql = "SELECT id, panel, place from `".LENTELES_PRIESAGA."panel` WHERE align='L' order by place";
	$recordSet = mysql_query1($sql);
	$listArray = array();
	while ($record = mysql_fetch_assoc($recordSet)) {
		$listArray[] = sprintf($listItemFormat,$record['id'],$record['panel'],$record['id'],$record['id'],$record['id']);
	}
	$sql1 = "SELECT id, panel, place from `".LENTELES_PRIESAGA."panel` WHERE align='R' order by place";
	$recordSet1 = mysql_query1($sql1);
	$listArray1 = array();
	while ($record1 = mysql_fetch_assoc($recordSet1)) {
		$listArray1[] = sprintf($listItemFormat,$record1['id'],$record1['panel'],$record1['id'],$record1['id'],$record1['id']);
	}
	mysql_free_result($recordSet);
	mysql_free_result($recordSet1);
	$listHTML = implode("\n",$listArray);$listHTML1 = implode("\n",$listArray1);

	$text = $sortableLists->SLLists("javascript");	//kelias iki js failu
	$text .= $sortableLists->printTopJS();				//atspauzdinam pagrindinius javascript

	$text .= '
		<table width="100%"><tr><td valign="top">
			<fieldset><legend>Kairė</legend>
			<ul class="sortableList" id="kaire">'.$listHTML.'</ul>';
	$text .= $sortableLists->printBottomJS();
	$text .= '</fieldset></td>
	<td valign="top"><fieldset><legend>Dešinė</legend>
		<ul class="sortableList" id="desine">'.$listHTML1.'</ul>';
	$text .= $sortableLists->printBottomJS();
	$text .= "</fieldset>
		</td>
	</tr>
	</table>";
	$text .= "<button onClick=\"window.location='?id,46;a,9;n,1';\">Nauja panelė</button>";
	$text .= $sortableLists->printForm('?id,46;a,9;p,l', 'POST', 'Saugoti', 'button');
	lentele('Blokų administravimas',$text);
	
	
//Funkcija panelių turiniui įrašyti
function irasom($Failas,$Info) {
	global $url;
	if (is_writable($Failas)) {
		if ($fh = fopen($Failas,'w')) {
			$Info = "<?php\n\$text = \"\n".addslashes($Info)."\n\";?>";
			if (fwrite($fh, $Info) !== FALSE) {
				msg('Failas atnaujintas','Panelės turinys sėkmingai atnaujintas');
				fclose($fh);
				redirect("?id,".$url['id'].";a,".$url['a'],"meta");
			}
		}
		else { klaida('Atsiprašome','Sistemos klaida. Negaliu atidaryti failo.'); }
	}
	else { klaida($Failas,'Sistemos klaida. Negaliu išsaugoti duomenų. Netinkamos teisės'); }
}
?>